# include "hashFunctions.h"

# define NADA ' '

static float probChar(char caracter);
static char cleanChar(char caracter);

int hashSimpleInteger (void *key, int max) {
        if (!key) return -1;
        else return (*(int *)key)%max;
}

static float probChar (char caracter) {
	switch (caracter) {
		case 'a': return 0.143;
		case 'b': return 0.009;
		case 'c': return 0.041;
		case 'd': return 0.049;
		case 'e': return 0.128;
		case 'f': return 0.012;
		case 'g': return 0.013;
		case 'h': return 0.011;
		case 'i': return 0.062;
		case 'j': return 0.008;
		case 'l': return 0.031;
		case 'm': return 0.043;
		case 'n': return 0.044;
		case 'o': return 0.111;
		case 'p': return 0.023;
		case 'q': return 0.011;
		case 'r': return 0.062;
		case 's': return 0.071;
		case 't': return 0.051;
		case 'u': return 0.052;
		case 'v': return 0.019;
		case 'x': return 0.002;
		case 'z': return 0.004;
	}
	return 0;
}

static char cleanChar (char caracter) {
	char c;
	if (!isalpha(caracter)) return NADA;
	else {
		if (isupper(caracter)) c = tolower(caracter);
		else c = caracter;
		if (c != 'k' && c != 'w' && c != 'y') return c;
		else return NADA;
	}
}

int portugueseHash (void *key, int size) {
	char *chave = (char *) key;
	int i,n = strlen(chave);
	float res = 0;
	char tmp;
	for (i=0;i<n;i++) {
		tmp = cleanChar(chave[i]);
		if (tmp != NADA) {
			res += 1/probChar(tmp);
		}
	}
	return (int) res % size;
}
